<?php
/**
 * 奖品管理
 * http://baseAddress/api/prize/
 */

namespace App\Http\Controllers\Api\Prize;

use App\App\Services\StorageService;
use App\App\Services\Web\UserService;
use App\Http\Controllers\Controller;
use App\Models\Prize\Prize;
use App\Models\Prize\PrizeService;
use App\Models\PrizeType\PrizeType;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class PrizeController extends Controller
{
    /**
     * 按条件查询奖品信息
     * @param Request $request 查询条件数组，数据key为prizes表的字段名
     * @param PrizeService $prizeService
     * @param StorageService $storageService
     * @param UserService $userService
     * @return \Illuminate\Http\JsonResponse
     */
    public function query(Request $request, PrizeService $prizeService, StorageService $storageService, UserService $userService)
    {
        $requestData = $request->query->get('data', '');
        if (!$requestData) {
            $requestData = $request->query->all();
        } else {
            $requestData = json_decode($requestData, true);
        }
        $conditions = array(
            'where' => array(),
            'whereNotIn' => array(),
            'whereIn' => array(),
            'orWhere' => array(),
            'whereNull' => array(),
            'whereNotNull' => array(),
            'whereBetween' => array(),
            'whereNotBetween' => array()
        );
        if (!empty($requestData['search'])) {
            $conditions['where'][] = array('name', 'like', '%' . $requestData['search'] . '%');
        }
        switch (@$requestData['category']) {
            case '1':
                $conditions['where'][] = array('type_code', '!=', PrizeType::ALIPAY);
                break;
            case '2':
                $conditions['where'][] = array('type_code', '!=', PrizeType::WECHAT);
                break;
        }
        if (!empty($requestData['exclude'])) {
            $prizeIds = explode(',', $requestData['exclude']);
            $conditions['whereNotIn'] = array('id', $prizeIds);
        }
        $userInfo = $userService->getLoginUser();
        $conditions['where'][] = array('user_id', $userInfo['id']);
        $conditions['where'][] = array('is_valid', Prize::IS_VALID_YES);
        $conditions['where'][] = ['group_code', $userInfo['groupcode']];
        $conditions['orWhere'][] = array('expired_end', '>', date('Y-m-d H:i:s'));
        $conditions['orWhere'][] = array('expired_end', null);
        $limit = @$requestData['limit'] ?: 10;
        $offset = @$requestData['offset'] ?: 0;
        $prizeInfos = $prizeService->getPaginateByConditions($conditions, array('prizeType', 'sponsor', 'activity', 'prizeRecord'), $limit, $offset);
        if ($prizeInfos) {
            $row = $prizeInfos['data'];
            foreach ($row as &$item) {
                $item['background_image'] = $storageService->tomedia($item['background_image']);
            }
            unset($item);
            $total = $prizeInfos['total'];
            return $this->respondJson(array('rows' => $row, 'total' => $total));
        } else {
            return $this->respondJson(array('prizes' => array(), 'code' => 1));
        }
    }

    /**
     * 编辑奖品信息
     * @param Request $request
     * @param PrizeService $prizeService
     * @return \Illuminate\Http\JsonResponse
     */
    public function modify(Request $request, PrizeService $prizeService)
    {
        $prizeInfo = $request->input('prize');
        if (is_string($prizeInfo)) {
            $prizeInfo = json_decode(str_replace('\'', '"', $prizeInfo), true);
        }
        $conditions = $request->input('conditions');
        if (is_string($conditions)) {
            $conditions = json_decode(str_replace('\'', '"', $conditions), true);
        }
        $prizeInfo = $prizeService->modify($prizeInfo, $conditions);
        if ($prizeInfo) {
            return $this->respondJson(array('prize' => $prizeInfo));
        } else {
            return $this->respondJson(array('prize' => array(), 'code' => 1));
        }
    }

    /**
     * 删除奖品
     * @param Request $request
     * @param PrizeService $prizeService
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(Request $request, PrizeService $prizeService)
    {
        $data = $request->input('data');
        $conditions = json_decode($data, true);
        $prizeInfo = $prizeService->delete($conditions, false);
        if ($prizeInfo) {
            return $this->respondJson(array('result' => 'success'));
        } else {
            return $this->respondJson(array('result' => 'fail', 'code' => 1));
        }
    }

    /**
     * 获取奖品使用情况
     * @param Request $request
     * @param PrizeService $prizeService
     * @return \Illuminate\Http\JsonResponse
     */
    public function settle(Request $request, PrizeService $prizeService)
    {
        $data = $request->request->get('data');
        $conditions = json_decode($data, true);
        $prizeInfos = $prizeService->settleActivity($conditions);
        return $this->respondJson(array('result' => $prizeInfos));
    }
}
